import Vue from 'vue'
import VueRouter from 'vue-router'

// import Login from '../views/Login'
// import Layout from '../views/Layout'
// import Home from '../views/Home'
// import User from '../views/User'
// import Search from '../views/Search'
// import SearchResult from '../views/Search/SearchResult'
// import ArticleDetail from '../views/ArticleDetail'
// import UserEdit from '../views/User/UserEdit'
// import Chat from '../views/Chat'
import { getToken } from '../utils/token'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/layout'
  },
  {
    path: '/login',
    component: () => import('../views/Login')
  },
  {
    path: '/layout',
    component: () => import('../views/Layout'),
    redirect: '/layout/home',
    children: [
      {
        path: 'home',
        component: () => import('../views/Home')
      },
      {
        path: 'user',
        component: () => import('../views/User')
      }
    ]
  },
  {
    path: '/search',
    component: () => import('../views/Search')
  },
  {
    path: '/search_result/:kw',
    component: () => import('../views/Search/SearchResult')
  },
  { // 文章详情
    path: '/detail',
    component: () => import('../views/ArticleDetail')
  },
  {
    path: '/user_edit',
    component: () => import('../views/User/UserEdit')
  },
  {
    path: '/chat',
    component: () => import('../views/Chat')
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (getToken()?.length > 0 && to.path === '/login') {
    // next(false) //留在原地，什么都不写
    // 想要进入登录页面，不留在原地了，而是返回首页
    next('/layout/home')
  } else {
    next()
  }
})

export default router
